﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using WebCamera;
using WEBCamera.Web;

namespace Web
{
    /// <summary>
    /// Capture for Office 365 Site need to set child element attribute "div", "id", "ShellBodyAndFooterContainer". You can updated the element by "O365PageCamera.SetElemntValue" or "O365PageCamera.SetRelation" mathod.
    /// </summary>
    public class O365PageCamera : PageCamera
    {
        private List<BrowserPageProxy> _ProxyList = new List<BrowserPageProxy>();

        public static string Type { get; set; }

        public static string AttName { get; set; }

        public static string AttValue { get; set; }

        public static string Relation { get; set; }

        public void SetRelation(string relationValue)
        {
            if (!string.IsNullOrEmpty(relationValue))
            {
                Relation = relationValue;
            }
        }

        /// <summary>
        /// set the html element taget , attribute and value for element capture or combine capture.
        /// </summary>
        /// <param name="type">taget name</param>
        /// <param name="attName">attribute name</param>
        /// <param name="attValue">attribute value</param>
        public void SetElementValue(string type, string attName, string attValue)
        {
            Type = type;
            AttName = attName;
            AttValue = attValue;
        }

        /// <summary>
        /// Capture for Office 365 Site
        /// </summary>
        /// <param name="savePath">screenshoot save path</param>
        public void ShootO365(string savePath)
        {
            string _IDEle = Guid.NewGuid().ToString("D");
            string _IDBody = Guid.NewGuid().ToString("D");
            string basePath = AppDomain.CurrentDomain.BaseDirectory;
            string eleCapPath = Path.Combine(basePath, _IDEle + ".tmp");
            string bodyCapPath = Path.Combine(basePath, _IDBody + ".tmp");

            this.Shoot(eleCapPath);
            int sobX = CombinScreenShot.absoX;
            int sobY = CombinScreenShot.absoY;

            SetElementValue(null, null, null);
            this.Attach();
            this.Shoot(bodyCapPath);
            CombinScreenShot combScreen = new CombinScreenShot();
            combScreen.shootCombine(savePath, bodyCapPath, eleCapPath, sobX, sobY);
        }

        public new void Attach(string url)
        {
            this._ProxyList = BrowserPageFactoryO365.GetAllPages(url);  ///Updated for O365
        }

        /// <summary>
        /// Attach to active browser
        /// </summary>
        public new void Attach()
        {
            this._ProxyList = BrowserPageFactoryO365.GetAllPages();
        }

        public new void Attach(bool activePageOnly)
        {
            if (activePageOnly)
            {
                this._ProxyList.Clear();
                BrowserPageProxy activePage = BrowserPageFactoryO365.GetActivePage();
                if (activePage != null)
                {
                    this._ProxyList.Add(activePage);
                }
            }
            else
            {
                this._ProxyList = BrowserPageFactoryO365.GetAllPages();
            }
        }

        public new void Attach(IntPtr MainWindowHandle)
        {
            this._ProxyList = new List<BrowserPageProxy>();
        }

        public new void Shoot(string savePath)
        {
            int num = 0;
            string path = Path.Combine(Path.GetDirectoryName(savePath), Path.GetFileNameWithoutExtension(savePath));
            foreach (BrowserPageProxy proxy in this._ProxyList)
            {
                bool flag = false;
                if (proxy.Visible)
                {
                    flag = true;
                }
                else
                {
                    proxy.Visible = true;
                }
                int num2 = 0;
                while ((num2 * proxy.ScreenHeight) < proxy.Height)
                {
                    proxy.Y = (proxy.ScreenHeight - proxy.BrowserWindow.Edge) * num2;
                    num2++;
                }
                num2--;
                for (int i = 0; (i * proxy.ScreenWidth) < proxy.Width; i++)
                {
                    proxy.X = (proxy.ScreenWidth - proxy.BrowserWindow.Edge) * i;
                    for (int j = num2; j >= 0; j--)
                    {
                        proxy.Y = (proxy.ScreenHeight - proxy.BrowserWindow.Edge) * j;
                        proxy.PrintWindow();
                    }
                }
                if (!flag)
                {
                    proxy.Visible = false;
                }
                if (++num > 1)
                {
                    path = string.Concat(new object[] { path, "-", num, Path.GetExtension(savePath) });
                }
                else
                {
                    path = savePath;
                }
                proxy.CombineImage(path);
            }
        }
    }
}
